package mp

import (
	"duoke/model"
	"duoke/service"
	"duoke/tables"
	"duoke/utils/gconv"
	"duoke/utils/orm"
	"duoke/utils/request"
	"duoke/utils/response"
	"github.com/gin-gonic/gin"
)

var Group = new(groupCtl)

type groupCtl struct{}

func (ctl *groupCtl) List(c *gin.Context) {
	var req model.ReqList
	goods, _ := service.Request.Goods(c)
	order := make([]model.Order, 0)
	account, _ := service.Request.Account(c)
	err := orm.Db.Where("acid =? AND goods_id =? AND is_head = 1 AND pay_state = 1 AND refund_state = 0", account.Id, goods.Id).Limit(100).Find(&order)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	for index, item := range order {
		if item.GroupId > 0 {
			member, err := service.Group.Member(req, item.GroupId, false, "")
			if err != nil {
				response.Error(c, err.Error(), nil)
				return
			}
			order[index].Member = member
		}
	}
	response.Success(c, "", order)
}

func (ctl *groupCtl) Info(c *gin.Context) {
	req := request.JsonMap(c)
	if req["groupId"] == "" || req["groupId"] == nil {
		response.Error(c, "团ID缺失", nil)
		return
	}
	if req["groupId"] == "0" {
		response.Success(c, "", nil)
		return
	}
	group, _ := service.Group.Info(c, gconv.Int(req["groupId"]))
	response.Success(c, "", group)
}

func (ctl *groupCtl) Member(c *gin.Context) {
	req := request.JsonMap(c)
	var req2 model.ReqList
	if req["groupId"] == "" || req["groupId"] == nil {
		response.Error(c, "团ID缺失", nil)
		return
	}
	member, err := service.Group.Member(req2, gconv.Int(req["groupId"]), true, "id,name,nickname,avatar")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", member)
}

// 我的拼团
func (ctl *groupCtl) My(c *gin.Context) {
	goods, _ := service.Request.Goods(c)
	user, _ := service.Request.User(c)
	where := "goods_id = " + gconv.String(goods.Id) + " AND user_id =" + gconv.String(user.Id) + " AND pay_state =1"
	order, _ := service.Order.GetByWhere(where, "")
	response.Success(c, "", order)
}

// 设置团长
func (ctl *groupCtl) SetHead(c *gin.Context) {
	req := request.JsonMap(c)
	orderId := gconv.Int(req["orderId"])
	if orderId == 0 {
		response.Error(c, "订单ID不存在", nil)
		return
	}
	order := new(tables.Order)
	has, err := orm.Db.Where("id =? AND is_head =0 AND pay_state = 1", orderId).Get(order)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if !has {
		response.Error(c, "订单不存在", nil)
		return
	}
	orgHeadOrderId := order.GroupId
	orgHeadGroupMemberNum := order.GroupMemberNum
	order.GroupId = orderId
	order.GroupNum = order.GroupNum
	order.GroupMemberNum = 1
	order.GroupState = 0
	order.HeadUserId = order.UserId
	order.IsHead = 1
	affected, err := orm.Db.ID(orderId).Cols("group_id", "group_num", "group_member_num", "group_state", "head_user_id", "is_head").Update(order)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if affected == 0 {
		response.Error(c, "操作失败", nil)
		return
	}
	// 设置团长单状态
	headOrder := new(tables.Order)
	headOrder.GroupMemberNum = orgHeadGroupMemberNum - 1
	headOrder.GroupState = 0
	affected, err = orm.Db.ID(orgHeadOrderId).Cols("group_member_num", "group_state").Update(headOrder)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if affected == 0 {
		response.Error(c, "操作失败", nil)
		return
	}
	response.Success(c, "操作成功", nil)
}

// 升级团数量
func (ctl *groupCtl) Rise(c *gin.Context) {
	req := request.JsonMap(c)
	orderId := gconv.Int(req["groupId"])
	if orderId == 0 {
		response.Error(c, "团长单ID不存在", nil)
		return
	}
	headOrder := new(tables.Order)
	has, err := orm.Db.Where("id =? AND is_head =1 AND pay_state = 1", orderId).Get(headOrder)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if !has {
		response.Error(c, "团长单不存在", nil)
		return
	}
	if headOrder.GroupNum >= 100 {
		response.Error(c, "最大百人团", nil)
		return
	}
	order := new(tables.Order)
	order.GroupNum = headOrder.GroupNum + 1
	order.GroupState = 0
	affected, err := orm.Db.Where("group_id =?  AND pay_state = 1", headOrder.Id).Cols("group_num", "group_state").Update(order)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if affected == 0 {
		response.Error(c, "操作失败", nil)
		return
	}
	response.Success(c, "操作成功", nil)
}

// 团长单，降团数
func (ctl *groupCtl) Drop(c *gin.Context) {
	req := request.JsonMap(c)
	orderId := gconv.Int(req["groupId"])
	if orderId == 0 {
		response.Error(c, "团长单ID不存在", nil)
		return
	}
	headOrder := new(tables.Order)
	has, err := orm.Db.Where("id =? AND is_head =1 AND pay_state = 1", orderId).Get(headOrder)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if !has {
		response.Error(c, "团长单不存在", nil)
		return
	}
	if headOrder.GroupState == 1 {
		response.Error(c, "该团已满团，无法降团数", nil)
		return
	}
	if headOrder.GroupNum <= headOrder.GroupMemberNum {
		response.Error(c, "该团已满团，无法降团数", nil)
		return
	}
	order := new(tables.Order)
	order.GroupNum = headOrder.GroupNum - 1
	if headOrder.GroupNum-headOrder.GroupMemberNum == 1 {
		order.GroupState = 1
	}
	affected, err := orm.Db.Where("group_id =?  AND pay_state = 1", headOrder.Id).Cols("group_num", "group_state").Update(order)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if affected == 0 {
		response.Error(c, "操作失败", nil)
		return
	}
	response.Success(c, "操作成功", nil)
}
